class Solution {
public:
    int countSubstrings(string s) {
        int n = s.size();
        int dp[n][n];
        memset(dp, 0, sizeof(dp));
        int ans = 0;
        for(int i = n - 1; i >= 0; --i)
        {
            for(int j = i; j < n; ++j)
            {
                if(s[i] == s[j] && (j - i <= 1 || dp[i + 1][j - 1]))
                {
                    ++ans;
                    ++dp[i][j];
                }
            }
        }
        return ans;
    }
};
